using Moq;
using NUnit.Framework;
using RMH.Entities;
using RMH.Web.Controllers;
using RMH.Services;
using RMH.Web.Models.Binders;

namespace RMH.UnitTests.Web.Binders
{
    [TestFixture]
    public class PatientBinder_when_multiple_diagnosis_are_on_the_form : base_binding_test<PatientController>
    {
        private int patientId = 23;

        Diagnosis diagnosis = new Diagnosis { Id = 1 };
        Diagnosis diagnosis2 = new Diagnosis { Id = 2};
        Patient patient = new Patient();
        private Patient returnedPatient;

        public override void establish_context()
        {
            form.Add("Id", patientId.ToString());
            form.Add("Diagnosis", "1,223");

            patient.Diagnoses.Add(diagnosis);
            patient.Diagnoses.Add(new Diagnosis { Id =22 });
            
            Mock<IPatientService>()
                .Setup(s => s.Get(23))
                .Returns(patient);

            Mock<IDiagnosisService>()
                .Setup(s => s.Get(1))
                .Returns(diagnosis);

            Mock<IDiagnosisService>()
                .Setup(s => s.Get(2))
                .Returns(diagnosis2);
            
            context.HttpRequest.Setup(r => r.Form).Returns(form);            
        }

        public override void because()
        {
            var binder = Create<PatientBinder>();
            returnedPatient = (Patient)getBindingResult(binder);
        }

        [Test]
        public void new_diagnosis_are_added_to_the_collection_and_existing_diagnosis_are_ignored()
        {
            returnedPatient.Diagnoses.Count.ShouldEqual(2);
            returnedPatient.Diagnoses.ShouldContain(diagnosis);
        }
    }
}